// ignore_for_file: unused_field


import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:async';

import 'package:qx_global_ads/qx_global_ads.dart';


void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  
  
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final String _platformVersion = 'Unknown';
  // final _qxGlobalAdsPlugin = QxGlobalAds();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    // String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    // try {
    //   platformVersion =
    //       await _qxGlobalAdsPlugin.getPlatformVersion() ?? 'Unknown platform version';
    // } on PlatformException {
    //   platformVersion = 'Failed to get platform version.';
    // }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      // _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HomeScreen(),
    );
  }
}


class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
   return Scaffold(
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: Center(
        child: Stack(
            children: [
              ListView(
                // mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Container(
                    padding: const EdgeInsets.symmetric(horizontal: 10),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(10),
                    ),
                    child: ClipRRect( // ✅ 这里包裹 BannerWidget
                      borderRadius: BorderRadius.circular(10), // ✅ 确保圆角生效
                      child: const BannerWidget(width: 350, height: 50, type:AdType.banner1),
                    ),
                  ),
                  const SizedBox(height: 50,),

                  ElevatedButton(
                    onPressed: () {
                        AdType type = AdType.interstitialPortrait;
                        if (MediaQuery.of(context).orientation == Orientation.portrait) {
                          type = AdType.interstitialPortrait;
                        } else {
                          type = AdType.interstitialLandscape;
                        }
                        showInterstitialAd(context,
                        type, (error) { 
                          debugPrint('Interstitial ad error: $error');
                        }, () { 
                          debugPrint('Interstitial ad closed');
                        }, () { 
                          debugPrint('Interstitial ad shown');
                        });
                    },
                    child: const Text('show interstitial ad'),
                  ),


                  ElevatedButton(
                    onPressed: () {
                      SystemChrome.setPreferredOrientations([
                        DeviceOrientation.portraitUp,
                        DeviceOrientation.landscapeLeft,
                      ]).then((_) {
                        if (MediaQuery.of(context).orientation == Orientation.portrait) {
                          SystemChrome.setPreferredOrientations([
                            DeviceOrientation.landscapeLeft,
                          ]);
                        } else {
                          SystemChrome.setPreferredOrientations([
                            DeviceOrientation.portraitUp,
                          ]);
                        }
                      });
                    },
                    child: const Text('switch screen orientation'),
                  ),

                  ElevatedButton(
                    onPressed: () async {
                      final adShowRate = await getAdShowRate('com.a1052.Holeio',isDebug: false);
                      // ignore: use_build_context_synchronously
                      ScaffoldMessenger.of(context).showSnackBar(
                        SnackBar(content: Text('ad show rate: $adShowRate'))
                      );

                    
                    },
                    child: const Text('get ad show rate'),
                  ),

                ],
              ),
              const GameRecommendWidget(isDebug: false,),
            ],
        ),  
      ),
    );
  }
}